<?php
function list_adjustment(){
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'TMB'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Gol'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Take Home Pay'=>array('style'=>'text-align:right;border-bottom:2px solid;width:35%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	$end=get_batas_tmt() ;
	if(isset($_GET['key'])){
		$query = "
			SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			AND ( c.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}' )
		";
	}else{	
		$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$query = " SELECT takehomepay FROM pr_gaji 
			WHERE periode_id=0
			AND karyawan_id=".$ey['karyawan_id']
		;
		$result1 = my_query($query);	
		$gajis = my_fetch_array($result1);
		if(	$gajis ){
			$takehomepay = $gajis['takehomepay'];	
		}else{
			$takehomepay = 0;
		}
		
		$detailproperty = array(
			'href'=>'index.php?com=pr_adjustment&task=detail&karyawan_id='.$ey['karyawan_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'gd'=>  $karyawan['nama_gelar'] , 
			'tmb'=> date('d-m-Y',strtotime($karyawan['tmb'])),
			'gol'=>  $karyawan['golongan_jabatan'] , 
			'kalkulasi'=>  position_text_align (rp_format($takehomepay ) ,'right'),      
			'operasi'=> position_text_align( $detail_button, 'right'),
		
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.get_tanggal_by_periode_id().'</span>'   , "<b>Periode Gaji</b>"    ); 
	$view  .= form_footer(  );
	$menuontop = array(  
		'Excel' => array('onclick'=>'location.href=\'index.php?com='.$_GET['com'].'&excel=1\''),
		'Kalkulasi' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_adjustment&task=kga\' });'),
		'THP Minus'=>array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment&task=thp_minus\''),
	);

	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana">
	<b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop ,$search ); 
	$paging = $kgPagerOBJ ->showPaging();
	return  $view.$printmenu.table_builder($header , $datas ,  6 , false , $paging  );

}

function list_adjustment_detail($karyawan_id){
	$karyawan = loaddata_karyawan($karyawan_id);
	$jabatan = my_get_data_by_id('jabatan' ,'jabatan_id' , ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id'] : 0 ));

	$gapok= $karyawan['gapok_km'] ;
	
	$tipes = array(
		'1'=>'Tunjangan',
		'2'=>'Potongan',
		'3'=>'Iuran',
	);		
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Tipe'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Rapel (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Total (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
	);
	
	$query = "SELECT a.komponen_id,b.nama_komponen_gaji,b.tipe,a.kalkulasi,a.rapel  
			FROM pr_gaji_komponen a 
			LEFT JOIN pr_komponen_gaji b ON a.komponen_id=b.komponen_gaji_id
			WHERE a.periode_id=0 
			AND (a.kalkulasi<>0  OR a.rapel<>0) 
			AND a.karyawan_id=".$karyawan_id
			." ORDER BY b.tipe, a.komponen_id"
	;
	$result = my_query($query);
	$row = array();
	$i=1;
	$total=$total_rapel=$total_rapel=$takehomepay=$thp_rapel=$thp_jumlah=0;
	
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$tanggal = $periode['tanggal'];	
	$bulan_gaji = (int) date('m' , strtotime($tanggal));
	$tahun_gaji = (int) date('Y' , strtotime($tanggal));	
	$gaji_dibayar=get_gaji_dibayar($gapok,$karyawan['tmb']);
	$query = "SELECT a.rapel  
			FROM pr_gaji a 
			WHERE a.periode_id=0 
			AND a.rapel<>0 
			AND a.karyawan_id={$karyawan_id} 
			LIMIT 1"
	;
	$res_gaji = my_query($query);
	$rapels=my_fetch_array($res_gaji)	;	
	$gaji_rapel=$rapels['rapel'];
			
/*
	GAJI DIBAYAR
 					  
    sfline( 'Proses TRANS & PERUM Karyawan Baru' )
          ***** Proses Tranport Karyawan baru
          while !xPay2->(eof())
                Mistar(@nSkip, nCount, .f., 20, 1 )
                if val(xPay2->NIK)<9999990
                   if xPay2->TYPE=='A' .and. xPay2->CODEA=='TRAN '
                      nTrans := val( xPay2->VALUE )
                      if empty(IPMAS->RESIGN)
                         recadd( "xPay2", "RL" )
                         nDayOut := 0
              *          nJHari := fJHari( fCtoN(nBl), fCtoN(nYear) )
                         if month(IPMAS->HIRE) == nBl .and. year(IPMAS->HIRE)==nYear
                            if day(IPMAS->HIRE)>1
                               for i = 1 to (day(IPMAS->HIRE)-1)
                                   nDayOut += if( upper( cDow( ctod( fNol( i, 2 ) + '/' + fNol( fCtoN(nBl), 2 ) + '/' + fNol( fCtoN(nYear), 4) ) ) )$'SATURDAYSUNDAY', 0, 1 )
                               next i
                               if nDayOut > 0
                                  xPay2->VALUE := str( round( nTrans - round( nDayOut * 8 / 173 * nTrans,0),0) )
                               endif
                            endif
                         endif
                      endif
                      xPay2->(dbunlock())
                   endif
                   if xPay2->TYPE=='A' .and. xPay2->CODEA=='PERUM'
                      if empty(IPMAS->RESIGN)
                         recadd( "xPay2", "FL" )
                         if IPMAS->STATUS #'1'
                            if ctod( '01/'+STR(month(IPMAS->HIRE),2)+'/'+STR(Year(IPMAS->HIRE),4) ) <= ctod( '01/'+alltrim(str(if(nBl-6<=0,12+nBl-6,nBl-3)))+'/'+alltrim(str(if(nBl-6<=0,nYear-1,nYear))))
                            else
                               xPay2->(dbdelete())
                            endif
                         endif
                      endif
                      xPay2->(dbunlock())
                   endif
                endif
                xPay2->(dbskip())
          enddo					  
	
*/

	
	
	$takehomepay=$gaji_dibayar ;
	$thp_rapel=$gaji_rapel ;
	$thp_jumlah=$gaji_dibayar+$gaji_rapel ;
	
	$row[] = array(
		'e'=>position_text_align($i,'center'),
		'nama'=>'Gaji Dibayarkan' ,
		'tipe'=>' ',
		'nom'=> position_text_align(rp_format($gaji_dibayar), 'right' ),
		'rapel'=> position_text_align('', 'right' ),
		'jumlah'=> position_text_align(rp_format($thp_jumlah), 'right' ),
		);
	if($gaji_rapel > 0){
		$i++;
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>'Rapel Gaji Dibayarkan' ,
			'tipe'=>' ',
			'nom'=> position_text_align(rp_format($gaji_rapel), 'right' ),
			'rapel'=> position_text_align('', 'right' ),
			'jumlah'=> position_text_align(rp_format($gaji_rapel), 'right' ),
		);
	}
	$tipe=1;
	while($ey = my_fetch_array($result)){
		if($ey['tipe']<>$tipe){
			$row[] = array(
				'e'=>' ',
				'nama'=>'Total '.$tipes[$tipe] ,
				'tipe'=>' ',
				'nom'=> position_text_align(rp_format($total), 'right' ),
				'rapel'=> position_text_align('', 'right' ),
				'jumlah'=> position_text_align(rp_format($total_jumlah), 'right' ),
			);	
			
			if($total_rapel > 0){
				$row[] = array(
					'e'=>' ',
					'nama'=>'Total rapel '.$tipes[$tipe] ,
					'tipe'=>' ',
					'nom'=> position_text_align(rp_format($total_rapel), 'right' ),
					'rapel'=> position_text_align('', 'right' ),
					'jumlah'=> position_text_align(rp_format($total_rapel), 'right' ),
				);	
			}
			$total=$total_rapel=$total_jumlah=0;
			$tipe=$ey['tipe'];
		}
		$i++;
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>$ey['nama_komponen_gaji'] ,
			'tipe'=>$tipes[$ey['tipe']],
			'kal'=> position_text_align(rp_format($ey['kalkulasi']), 'right' ),
			'rapel'=> position_text_align('', 'right' ),
			'jumlah'=> position_text_align(rp_format($ey['kalkulasi']+$ey['rapel']), 'right' ),
		);
		if($ey['rapel'] > 0){
			$row[] = array(
				'e'=>position_text_align($i,'center'),
				'nama'=>'<b>Rapel</b> '. $ey['nama_komponen_gaji'] ,
				'tipe'=>$tipes[$ey['tipe']],
				'kal'=> position_text_align(rp_format($ey['rapel']), 'right' ),
				'rapel'=> position_text_align('' , 'right' ),
				'jumlah'=> position_text_align(rp_format( $ey['rapel']), 'right' ),
			);
		}
		if($ey['tipe']<3){	
			$takehomepay+=$ey['kalkulasi'];
			$thp_jumlah+=$ey['kalkulasi'];
			$thp_rapel+=$ey['rapel'];
			$thp_jumlah+=$ey['rapel'];
		}	
		$total+=$ey['kalkulasi'];
		$total_jumlah+=$ey['kalkulasi'];
		$total_rapel+=$ey['rapel'];
		$total_jumlah+=$ey['rapel'];
	}
	$row[] = array(
		'e'=>' ',
		'nama'=>'Total '.$tipes[$tipe],
		'tipe'=>' ',
		'nom'=> position_text_align(rp_format($total + $total_rapel), 'right' ),
		'rapel'=> position_text_align('', 'right' ),
		'jumlah'=> position_text_align(rp_format($total_jumlah), 'right' ),
	);		
	$row[] = array(
		'e'=>' ',
		'nama'=>'Take Home Pay' ,
		'tipe'=>' ',
		'nom'=> position_text_align(rp_format($takehomepay), 'right' ),
		'rapel'=> position_text_align(rp_format($thp_rapel), 'right' ),
		'jumlah'=> position_text_align(rp_format($thp_jumlah), 'right' ),
	);
	$datas = table_rows($row);
	$newline=array(
		'Gaji Pokok'=>'Rp. '.rp_format($gapok)  
	);
	
	$pro =   karyawan_header_detail($karyawan,true, $newline  );
	$menuontop = array(  
		'Kalkulasi' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment&task=kalkulasi_gaji&karyawan_id='.$karyawan_id.'\''),
		'Pajak'=>array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment&task=pajak&karyawan_id='.$karyawan_id.'\''),
		'Back' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment'.'\'')
	);

	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );
}

function list_kalkulasi_pajak($karyawan_id){
	$karyawan = loaddata_karyawan($karyawan_id);
	$query = " SELECT * FROM pr_gaji_pajak 
		WHERE periode_id=0
		AND karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$pajaks = my_fetch_array($result);	
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen Pajak'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	
	$query = "SELECT * FROM pr_komponen_pajak
			ORDER BY komponen_pajak_id"
	;
	$result = my_query($query);
	$row = array();
	$i=0;

	while($ey = my_fetch_array($result)){
		$i++;
		$detailproperty = array(
			'href'=>'index.php?com=pr_adjustment&task=rinci&karyawan_id='.$karyawan_id.
				'&komponen_id='.$ey['komponen_pajak_id'],
			'title'=>'Detail Rinci Pajak'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$nominal=$pajaks['pajak_'.sprintf('%02d' ,$i)];
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>$ey['label_pajak'] ,
			'nom'=> position_text_align(rp_format($nominal), 'right' ),
			'op'=> position_text_align($detail_button , 'right' ),
		);
	}
	
	$datas = table_rows($row); 
	$pro =   karyawan_header_detail($karyawan,true  );
	
	$menuontop = array(  
		'Back' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment&task=detail&karyawan_id='.$karyawan_id.'\'')
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );
}

function get_pr_gaji_by_karyawan_id($karyawan_id){
	$query = " SELECT * FROM pr_gaji 
		WHERE periode_id=0
		AND karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$gajis = my_fetch_array($result);
	return  $gajis;
}

function list_rinci_pajak($karyawan_id,$komponen_id){
	$query = " SELECT gapok_km FROM kekaryawanan 
		WHERE karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$gajis = my_fetch_array($result);	
	$gapok= $gajis['gapok_km'] ;
	$karyawan = loaddata_karyawan($karyawan_id);
	$gaji_dibayar=get_gaji_dibayar($gapok,$karyawan['tmb']);
	$karyawan_status_id = $karyawan['status_kekaryawan_id'];
	$jabatan = my_get_data_by_id('jabatan' ,'jabatan_id' , ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id'] : 0 ));
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Komponen Gaji'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:30%'),  
	);
	$query = "SELECT a.nama_komponen_gaji, b.kalkulasi,
		a.komponen_pajak_id,a.komponen_pajak_id1 
		FROM pr_komponen_gaji a
		INNER JOIN pr_gaji_komponen b ON b.periode_id=0 
		 AND a.komponen_gaji_id=b.komponen_id 
		 AND b.karyawan_id={$karyawan_id}
		WHERE a.komponen_pajak_id=".$komponen_id." 
		OR a.komponen_pajak_id1=".$komponen_id ;
	$result = my_query($query);	
	$row = array();
	$i=0;
	$total=0 ;
	if($komponen_id==1){
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>'Gaji Pokok' ,
			'nom'=> position_text_align(rp_format(
				$gaji_dibayar), 'right' ),
		);	
		$total+=$gaji_dibayar	;
	}
	while($ey = my_fetch_array($result)){
		$i++;
		if($ey['komponen_pajak_id']==12){
			$temp=$ey['kalkulasi']*-1 ;
		}else{	
			if($ey['komponen_pajak_id1']==12){
				$temp=$ey['kalkulasi'] ;
			}else{	
				$temp=$ey['kalkulasi']*-1 ;
			}	

		$temp=$ey['kalkulasi'] ;
		}	
		$row[] = array(
			'e'=>position_text_align($i,'center'),
			'nama'=>$ey['nama_komponen_gaji'] ,
			'nom'=> position_text_align(rp_format(
				$temp ), 'right' ),
		);
		$total+=$temp;
	}
	$row[] = array(
		'e'=>' ',
		'nama'=>' T O T A L' ,
		'nom'=> position_text_align(rp_format(
			$total ), 'right' ),
	);
	
	$datas = table_rows($row);
	$pro =   karyawan_header_detail($karyawan  );
	
	$menuontop = array(  
		'Back' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment&task=pajak&karyawan_id=13&karyawan_id='.$karyawan_id.'\'')
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );
}	
/*
=========================================
		HITUNG GAJI
=========================================
*/
function kalkulasi_gaji($karyawan_id,$konstan){
	$should_periode_id = get_periode_gaji();
	$periodes=my_get_data_by_id('pr_periode','periode_id',$should_periode_id) ;
	$tanggal=$periodes['tanggal'] ;
	
	$query = " SELECT gapok_km FROM kekaryawanan 
		WHERE karyawan_id=".$karyawan_id
	;
	$result = my_query($query);	
	$gajis = my_fetch_array($result);	
	
	$karyawan = loaddata_karyawan($karyawan_id);
	$gaji_dibayar=get_gaji_dibayar( $gajis['gapok_km'],$karyawan['tmb']);	
	$gapok_km=$gajis['gapok_km'] ;
	//RAPEL
	$query = "SELECT * FROM pr_rapel_bayar WHERE periode_bayar='{$tanggal}' and karyawan_id={$karyawan_id} LIMIT 1";

	$res_rapel = my_query($query);
	$ey_rapel = my_fetch_array($res_rapel);
	if($ey_rapel){
		$rapel_periods=my_get_data_by_id('pr_rapel_periode','rapel_periode_id',$ey_rapel['rapel_periode_id']);
		$rapel_id=$rapel_periods['rapel_id'];
		$periode_gaji=$rapel_periods['periode_gaji'];
		$tase=$ey_rapel['tase'];
		$rapel=$ey_rapel['rapel'];
	}else{	
		$rapel_id=0;
		$rapel='';
	}	
	$takehomepay=$gaji_dibayar ;
	$komponen_pajak = array();
	$komponen_pajak[1] = $gaji_dibayar ;
	$komponen_pajak[2]=0;
	$komponen_pajak[3]=0;
	$komponen_pajak[4]=0;
	$komponen_pajak[5]=0;
	$komponen_pajak[6]=0;
	$komponen_pajak[8]=0;
	$komponen_pajak[12]=0;

	$karyawan_status_id = $karyawan['status_kekaryawan_id'];
	$st = array(
		'1'=>'Organik',
		'2'=>'Kontrak',
		'3'=>'Perbantuan',
		'4'=>'Alih_Status',
	);
	
	//CEK PERAWAT
	if($karyawan['profesi_detail_id']==0){
		$query_perawat=" AND model_entry<> 4 ";
	}else{
		$query_perawat="" ;
	}
	
	$query = "SELECT * FROM pr_penyesuaian
		WHERE karyawan_id=".$karyawan_id ;
	$komponen_penyesuaian=array();
	$result = my_query($query); 
	while($ey = my_fetch_array($result)){
		$komponen_penyesuaian[]=$ey['komponen_id'];
	} 
	
	//CHECK MEDICAL
	$medicals = array(1,2); 
	
	
	$status = strtolower($st[$karyawan_status_id]);
	$status = (trim($status) <> '') ? $status : $st[1];
		 
	$query = "SELECT komponen_gaji_id,tipe,komponen_pajak_id,komponen_pajak_id1,direksi_null FROM pr_komponen_gaji
		WHERE (model_entry<>2 OR
		( model_entry=2 AND ".$status."='yes'))".$query_perawat;
		 
	$result = my_query($query); 
	while($ey = my_fetch_array($result)){
		
		if(in_array($ey['komponen_gaji_id'] , $komponen_penyesuaian)){
			$kalkulasi = get_komponen_pengecualian($ey['komponen_gaji_id'] , $karyawan_id ); 
			
		}else{
			$kalkulasi = get_komponen_gaji($ey['komponen_gaji_id'] , $karyawan_id  , $konstan);
			
		}
	 
		if( $ey['direksi_null'] == 'Y' ){
		
			if(	get_status_direktur_by_karyawan_id($karyawan_id)	)$kalkulasi = 0;
			if( $karyawan['direktur_perbantuan'] == 'Y' )$kalkulasi = get_komponen_pengecualian($ey['komponen_gaji_id'] , $karyawan_id ); 
		}
		
		$total=$kalkulasi;
		
		if((int) $ey['tipe']==2){
			$kalkulasi =$kalkulasi*-1 ;
			$total=$total*-1 ;
		}	

		if((int) $ey['tipe']<>3){
			$takehomepay+=$total ;
		}	


		if($ey['komponen_pajak_id']>0){
			if($ey['komponen_pajak_id']==12){ //IURAN PENSIUN ATAU IURAN THT/ JHT
				$komponen_pajak[$ey['komponen_pajak_id']]-=$total;				
			}else{	
				$komponen_pajak[$ey['komponen_pajak_id']]+=$total;
			}	
		}
		if($ey['komponen_pajak_id1']>0){
			if($ey['komponen_pajak_id1']==12){ //IURAN PENSIUN ATAU IURAN THT/ JHT
				$komponen_pajak[$ey['komponen_pajak_id1']]+=$total;				
			}else{	
				$komponen_pajak[$ey['komponen_pajak_id1']]-=$total;
			}	
		}

		//RAPEL
		$rapel_komponen='';
		if($rapel_id==0){
		}else{
			$query = "SELECT * FROM pr_rapel_komponen WHERE rapel_id={$rapel_id} and komponen_id={$ey['komponen_gaji_id']}";
			$res_komponen = my_query($query);
			if( my_num_rows($res_komponen)>0){
				$rapel_komponen=get_rapel_komponen($karyawan_id,$periode_gaji,$ey['komponen_gaji_id'],$tase , $kalkulasi);
			}	
		}
		if(trim($kalkulasi)=='0' AND trim($rapel_komponen)=='0'){
		}else{	
			if( (int) $kalkulasi <> 0){
				 
			$datas = array(
				'periode_id'=>my_type_data_int(0),
				'karyawan_id'=>my_type_data_int($karyawan_id), 
				'komponen_id'	=>my_type_data_int($ey['komponen_gaji_id']), 			
				'kalkulasi'	=>my_type_data_str($kalkulasi), 
				'rapel'	=>my_type_data_str($rapel_komponen), 
			);
			my_insert_record('pr_gaji_komponen' , $datas);
			}
		}
	}
	//pajak
	$komponen_pajak[7]=$komponen_pajak[1]
		+$komponen_pajak[2]
		+$komponen_pajak[3]
		+$komponen_pajak[4]
		+$komponen_pajak[5]
		+$komponen_pajak[6] ;
	$komponen_pajak[9]=$komponen_pajak[7]
		+$komponen_pajak[8] ;

	//BIAYA JABATAN
	$persen_bj = get_kolom_pajak(19); 
	$max_bj = get_kolom_pajak(20)/12; 
	
	//BIAYA JABATAN ANGKA 7
	$kal_persen_bj =0.01 * $persen_bj * $komponen_pajak[7];
	if( $kal_persen_bj > $max_bj ) 
		$komponen_pajak[10] = $max_bj;
	else  
	$komponen_pajak[10] = $kal_persen_bj; 
	
	//BIAYA JABATAN ANGKA 8
	$kal_persen_bj =0.01 * $persen_bj * $komponen_pajak[8];
	if( $kal_persen_bj > $max_bj ) 
		$komponen_pajak[11] = $max_bj;
	else  
		$komponen_pajak[11] = $kal_persen_bj; 
	$komponen_pajak[13]=$komponen_pajak[10]+$komponen_pajak[11]+$komponen_pajak[12]	;
	$komponen_pajak[14]=$komponen_pajak[9]-$komponen_pajak[13]	;
	$komponen_pajak[15]=0;

	//disetahunkan
	$komponen_pajak[16]=($komponen_pajak[14]-$komponen_pajak[15])*12; 
	
	$query = "
		SELECT value FROM pr_konfigurasi_pajak
		WHERE kolom_id = 16";
	$ptkps = my_fetch_array(my_query($query));
	$ptkp=$ptkps['value'];
	if($karyawan['ptkp_status']=='K'){
		$query = "
			SELECT value FROM pr_konfigurasi_pajak 
			WHERE kolom_id = 17";
		$ptkps = my_fetch_array(my_query($query));
		$ptkp+=$ptkps['value'];
	}
	$query = "
		SELECT value FROM pr_konfigurasi_pajak 
		WHERE kolom_id = 18";
	$ptkps = my_fetch_array(my_query($query));
	$tanggung=MIN($karyawan['ptkp_tanggung'],3);
	$ptkp+=($ptkps['value']*$tanggung);
	$komponen_pajak[17]=$ptkp;
	$komponen_pajak[18]=$komponen_pajak[16]-$komponen_pajak[17];
	//UPDATE PR_GAJI_PAJAK BEFORE TAX	
	$datas = array(
		'periode_id'=>my_type_data_int(0),
		'karyawan_id'=>my_type_data_int($karyawan_id), 
		'pajak_01'	=>my_type_data_str($komponen_pajak[1]), 			
		'pajak_02'	=>my_type_data_str($komponen_pajak[2]), 			
		'pajak_03'	=>my_type_data_str($komponen_pajak[3]), 			
		'pajak_04'	=>my_type_data_str($komponen_pajak[4]), 			
		'pajak_05'	=>my_type_data_str($komponen_pajak[5]), 			
		'pajak_06'	=>my_type_data_str($komponen_pajak[6]), 			
		'pajak_07'	=>my_type_data_str($komponen_pajak[7]), 			
		'pajak_08'	=>my_type_data_str($komponen_pajak[8]), 			
		'pajak_09'	=>my_type_data_str($komponen_pajak[9]), 			
		'pajak_10'	=>my_type_data_str($komponen_pajak[10]), 			
		'pajak_11'	=>my_type_data_str($komponen_pajak[11]), 			
		'pajak_12'	=>my_type_data_str($komponen_pajak[12]), 			
		'pajak_13'	=>my_type_data_str($komponen_pajak[13]), 			
		'pajak_14'	=>my_type_data_str($komponen_pajak[14]), 			
		'pajak_15'	=>my_type_data_str($komponen_pajak[15]), 			
		'pajak_16'	=>my_type_data_str($komponen_pajak[16]), 			
		'pajak_17'	=>my_type_data_str($komponen_pajak[17]), 			
		'pajak_18'	=>my_type_data_str($komponen_pajak[18]), 			
	);
	my_insert_record('pr_gaji_pajak' , $datas);

	$pajak=get_tunjangan_pajak($karyawan_id);
	$komponen_pajak[2]+=$pajak ;
	$komponen_pajak[7]+=$pajak ;
	$komponen_pajak[9]+=$pajak ;
	//HITUNG ULANG
	$kal_persen_bj =0.01 * $persen_bj * $komponen_pajak[7];
	if( $kal_persen_bj > $max_bj ) 
		$komponen_pajak[10] = $max_bj;
	else  
	$komponen_pajak[10] = $kal_persen_bj; 
	
	//BIAYA JABATAN ANGKA 8
	$kal_persen_bj =0.01 * $persen_bj * $komponen_pajak[8];
	if( $kal_persen_bj > $max_bj ) 
		$komponen_pajak[11] = $max_bj;
	else  
		$komponen_pajak[11] = $kal_persen_bj; 
	$komponen_pajak[13]=$komponen_pajak[10]+$komponen_pajak[11]+$komponen_pajak[12]	;
	$komponen_pajak[14]=$komponen_pajak[9]-$komponen_pajak[13]	;	
	//disetahunkan
	$komponen_pajak[16]=($komponen_pajak[14]-$komponen_pajak[15])*12; 
	$komponen_pajak[18]=$komponen_pajak[16]-$komponen_pajak[17];
	
	$komponen_pajak[19]=get_pph21($komponen_pajak[18]) ;
	$komponen_pajak[21]=($komponen_pajak[19]-$komponen_pajak[20])/12 ;
	$komponen_pajak[21]=100 * ( floor( $komponen_pajak[21] /100) ) ;
	$komponen_pajak[22]=0 ;
	$komponen_pajak[23]=$komponen_pajak[21]-$komponen_pajak[22] ;

	//UPDATE PR_GAJI_PAJAK AFTER TAX	
	$query = "UPDATE pr_gaji_pajak SET 
		pajak_02 = '".$komponen_pajak[2]."' 
		,pajak_07 = '".$komponen_pajak[7]."' 		
		,pajak_09 = '".$komponen_pajak[9]."'
		,pajak_10 = '".$komponen_pajak[10]."'
		,pajak_11 = '".$komponen_pajak[11]."'
		,pajak_13 = '".$komponen_pajak[13]."'
		,pajak_14 = '".$komponen_pajak[14]."'
		,pajak_16 = '".$komponen_pajak[16]."'
		,pajak_18 = '".$komponen_pajak[18]."'
		, pajak_19 = '".$komponen_pajak[19]."' 
		, pajak_20 = '".$komponen_pajak[20]."' 
		, pajak_21 = '".$komponen_pajak[21]."' 
		, pajak_22 = '".$komponen_pajak[22]."' 
		, pajak_23 = '".$komponen_pajak[23]."' 
		WHERE periode_id=0
		AND karyawan_id=".$karyawan_id; 
	$dres = my_query($query);

	//Insert pr_gaji_komponen
	$query = "SELECT komponen_gaji_id FROM pr_komponen_gaji
		WHERE model_entry=5" ;
	$result = my_query($query);
	 while($tunjangan_pajaks=my_fetch_array($result) ){
		$datas = array(
				'periode_id'=>my_type_data_int(0),
				'karyawan_id'=>my_type_data_int($karyawan_id), 
				'komponen_id'	=>my_type_data_int($tunjangan_pajaks['komponen_gaji_id']), 			
				'kalkulasi'	=>my_type_data_str($pajak), 
			);
			my_insert_record('pr_gaji_komponen' , $datas);
/*			
		$query = "UPDATE pr_gaji_komponen SET 
			kalkulasi = '".$pajak."' 
			WHERE periode_id=0
			AND karyawan_id=".$karyawan_id."  
			AND komponen_id=".$tunjangan_pajaks['komponen_gaji_id'];		
		$dres = my_query($query);
*/		
	 }	
	$selisih_pembulatan=($takehomepay-floor($takehomepay/100)*100)*-1 ;
	$takehomepay+=$selisih_pembulatan;
	
	$query = " SELECT komponen_gaji_id FROM pr_komponen_gaji
		WHERE model_entry=7" ;
	$result = my_query($query);
	 while($tunjangan_selisihs=my_fetch_array($result) ){
		$datas = array(
				'periode_id'=>my_type_data_int(0),
				'karyawan_id'=>my_type_data_int($karyawan_id), 
				'komponen_id'	=>my_type_data_int($tunjangan_selisihs['komponen_gaji_id']), 			
				'kalkulasi'	=>my_type_data_str($selisih_pembulatan), 
			);
			my_insert_record('pr_gaji_komponen' , $datas);
	 }	
	 
	//update pr_gaji
	$query = "SELECT gaji_id 
			FROM pr_gaji
			WHERE periode_id=0
			AND karyawan_id=".$karyawan_id
	;
	$result = my_query($query);
	$gajis=my_fetch_array($result) ;

	if($gajis){
		$gaji_id=$gajis['gaji_id'];
	}else{
		$gaji_id=0;
	}
	$datas = array(
		'periode_id'=>my_type_data_int(0),
		'karyawan_id'=>my_type_data_int($karyawan_id), 
		'jabatan_id'=>my_type_data_int($karyawan['jabatan_id']), 
		'gapok_km'=>my_type_data_int($gapok_km), 
		'gapok_ks'=>my_type_data_int($karyawan['gapok_ks']), 
		'gaji_dibayar'=>my_type_data_int($gaji_dibayar), 
		'bank_id'=>my_type_data_int($karyawan['bank_id']), 
		'norek'=>my_type_data_str($karyawan['norek']), 
		'pajak'=>my_type_data_str($pajak), 
		'takehomepay'=>my_type_data_str($takehomepay), 
		'rapel'=>my_type_data_str($rapel), 	
		'ptkp_status'=>my_type_data_str($karyawan['ptkp_status']), 
		'ptkp_tanggung'=>my_type_data_str($karyawan['ptkp_tanggung']), 
		'status_insentif'=>my_type_data_str($karyawan['status_insentif']), 
		'premi_kesehatan'=>my_type_data_str($karyawan['premi_kesehatan']), 
		'golongan_jabatan_id'=>my_type_data_int($karyawan['golongan_jabatan_id']), 
		'grade_id'=>my_type_data_int($karyawan['grade_id']), 
		'status_id'=>my_type_data_int($karyawan['status_id']), 		
	);	
	if($gaji_id==0){
		my_insert_record('pr_gaji' , $datas);
	}else{	
		my_update_record('pr_gaji','gaji_id',$gaji_id, $datas);
	}	
	
	return $takehomepay;

}	



function total_alpha($karyawan_id ){
	$query = "SELECT COUNT(*) AS ttl FROM temp_tmn_hari WHERE karyawan_id = {$karyawan_id}   AND status_absen = 'A' "; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['ttl'];
}

function get_potongan_transport($karyawan_id ,$tanggal , $komponen_gaji_id){
	$potongan = (int) total_alpha($karyawan_id );
	if($potongan == 0) return 0;
	
	$komponen_gaji_id = 5;
	$sk_id=get_sk_id($tanggal,$komponen_gaji_id);
	$query = "
		SELECT nominal FROM pr_sk_tunjangan_detail a
		INNER JOIN kekaryawanan b ON b.golongan_jabatan_id = a.sub_id 
		WHERE b.karyawan_id = {$karyawan_id } 
		AND a.komponen_gaji_id={$komponen_gaji_id} 
		AND a.sk_id={$sk_id}
	";	
	
	
	$dres = my_query($query);
	$drow = my_fetch_array($dres);
	 
	return ( $drow['nominal'] * ( $potongan / 30 ) );
}

 
function get_komponen_gaji($komponen_gaji_id , $karyawan_id, $konstans){
	$hasil_type_entry = 0;
	$komponen = my_get_data_by_id( 'pr_komponen_gaji' , 'komponen_gaji_id' , $komponen_gaji_id );
	$periode = my_get_data_by_id('pr_periode','periode_id',get_periode_gaji());
	$tanggal = $periode['tanggal'];	

	if($komponen['model_entry'] == '1'){ //upload
		$query = "
			SELECT nominal FROM pr_gaji_upload_view 
			WHERE karyawan_id = {$karyawan_id } 
			AND komponen_gaji_id={$komponen_gaji_id}
		";	 
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry = $drow['nominal'];	 
	}elseif($komponen['model_entry'] == '2'){ //rumus
		$trr=formula_umum($komponen['formula'], $karyawan_id, $konstans);  
		if((int)strlen(trim($trr)) <> 0){			
			eval("\$hasil_type_entry  =  ".  $trr .";" ) ; 
		}	 
	}elseif($komponen['model_entry'] == '3'){  //jabatan
		$karyawansdata = loaddata_karyawan($karyawan_id);
		$sk_id=get_sk_id($tanggal,$komponen_gaji_id);
		$datask = my_get_data_by_id('pr_sk_tunjangan','sk_id',$sk_id);
		$query = "
			SELECT nominal FROM pr_sk_tunjangan_detail a
			INNER JOIN kekaryawanan b ON b.golongan_jabatan_id = a.sub_id 
			WHERE b.karyawan_id = {$karyawan_id } 
			AND a.komponen_gaji_id={$komponen_gaji_id} 
			AND a.sk_id={$sk_id}
		";	
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		
		if($datask['tmb_porsi']=='Y'){ 
			 
			$periode_range_out = explode("-", $periode['tanggal']);
			$periode_start 	= $periode_range_out[0] .'-'. $periode_range_out[1] .'-01';
			$periode_end 	= $periode_range_out[0] .'-'. $periode_range_out[1] .'-30';
			
			if( 
				strtotime($karyawansdata['tmb'] ) <= strtotime($periode_end)
				&&  
				strtotime($karyawansdata['tmb'] ) >= strtotime($periode_start) 
			){ 
				$diff = abs(strtotime($periode_end) - strtotime($karyawansdata['tmb']));
				$seleisih_hari = floor($diff / (60*60*24));  
			 
				$hasil_type_entry = $drow['nominal'] * ( $seleisih_hari / 30 ) ;
				$sdatas = array(
					'dtime'=>my_type_data_function('NOW()'),
					'notd'=>my_type_data_str( $drow['nominal'] .'-'.$seleisih_hari.'-"'.$karyawansdata['tmb'].'"['.$hasil_type_entry.']'),
				);
				my_insert_record('notew',$sdatas);
				
			}else{
				$hasil_type_entry = $drow['nominal'] ;
			}			
			
		}elseif($datask['tmb_plus_tujuh']=='Y'){
			$periode_range_out = explode("-", $periode['tanggal']);
			$periode_start = $periode_range_out[0] .'-'. sprintf('%02d',((int)$periode_range_out[0] - 1)) .'-11';
			$periode_end = $periode_range_out[0] .'-'.   $periode_range_out[0] .'-10';
			$diff = abs(strtotime($periode_end) - strtotime($karyawansdata['tmb']));
			$selisih_bulan = floor($diff / (30*60*60*24));			
			if( $selisih_bulan < 7 ){
				$hasil_type_entry = 0;
			}else{
				$hasil_type_entry = $drow['nominal']; 
			} 
		}else{
			$hasil_type_entry = $drow['nominal']; 
		}
		
	}elseif($komponen['model_entry'] == '4'){  //profesi
		$sk_id=get_sk_id($tanggal,$komponen_gaji_id);
		$query = "
			SELECT nominal FROM pr_sk_tunjangan_detail a
			INNER JOIN kekaryawanan b ON b.profesi_detail_id = a.sub_id 
			WHERE b.karyawan_id = {$karyawan_id } 
			AND a.komponen_gaji_id={$komponen_gaji_id} 
			AND a.sk_id={$sk_id}
		";	 
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry = $drow['nominal'];
	}elseif($komponen['model_entry'] == '5'){  //nilai pajak
		$hasil_type_entry = 0;		
	}elseif($komponen['model_entry'] == '6'){  //potongan limit waktu


		$query = "
			SELECT total,waktu,potongan FROM pr_potongan_limit
			WHERE komponen_gaji_id = {$komponen_gaji_id} 
			AND aktif = 'Y' 
			AND karyawan_id = {$karyawan_id } 
			AND '".$tanggal."' 
			BETWEEN tanggal_mulai AND tanggal_selesai
		";	
		$dres = my_query($query);
		$drow = my_fetch_array($dres);
		$hasil_type_entry =   ($drow['waktu'] > 0) ? ceil( $drow['total'] / $drow['waktu'] ) : $drow['potongan']  ;	
	}elseif($komponen['model_entry'] == '7'){  //selisih pembulatan
		$hasil_type_entry = 0;				
	}
	
	
	return round($hasil_type_entry,0);
}

function get_komponen_pengecualian($komponen_gaji_id , $karyawan_id){
	$query = "SELECT * FROM pr_penyesuaian 
		WHERE karyawan_id = {$karyawan_id} 
		AND komponen_id={$komponen_gaji_id}";
	$dres = my_query($query);
	$drow = my_fetch_array($dres);
	$penyesuaian = $drow['penyesuaian'];
	return $penyesuaian;
}

function get_tunjangan_pajak($karyawan_id){	
	$query = "SELECT * FROM pr_tarif_pajak
				ORDER BY label_range" ;
	$result=my_query($query);
	$arange_tmp=$arange=$atarif=$apersen=$apenambahan=$amaksimal=array();
	$max_tarif=-1;
	while($ey = my_fetch_array($result)){
		$max_tarif++;
		$atarif[$max_tarif]=$ey['tarif'];
		$arange_tmp[$max_tarif]=(int) $ey['label_range'];
		$apersen[$max_tarif]=$ey['tarif']/(100-$ey['tarif']) ;
		if($max_tarif>0){
			$arange[$max_tarif-1]=$arange_tmp[$max_tarif];
			$apenambahan[$max_tarif]=$atarif[$max_tarif-1]*0.01*
				($arange_tmp[$max_tarif]-$arange_tmp[$max_tarif-1])+$apenambahan[$max_tarif-1] ;
		}else{
			$apenambahan[$max_tarif]=0;
		}
		$amaksimal[$max_tarif]=$arange_tmp[$max_tarif]-$apenambahan[$max_tarif] ;	
		
	}

	$query = " SELECT * FROM pr_gaji_pajak 
			WHERE karyawan_id = {$karyawan_id } 
			AND periode_id=0" ;
	$pajaks = my_fetch_array(my_query($query));	
	$tmpData = $pajaks['pajak_18'];
	$tmpData = 100 * ( floor( $tmpData /100) ); 
	foreach($amaksimal as $key=>$value){
		if($tmpData >= $amaksimal[$key]){
			$nrange=$key;
		}
	}
	$tpj = ( $tmpData - $amaksimal[$nrange] ) * $apersen[$nrange] + $apenambahan[$nrange] ;
	unset($tmpData);
	$tpj = $tpj /12 ;
	$tpj = 100 * ( floor( $tpj /100) ); 	
	
//	Recalculation	

	$gross = 0;
	$persen_bj = get_kolom_pajak(19); 
	$max_bj = get_kolom_pajak(20)/12; 

	for( $y = 0; $y<=100; $y++){ 
		$tmpData = $pajaks['pajak_07'] + $tpj;
		 
		 if( ( $persen_bj * $tmpData * 0.01 ) > ( $max_bj  ) ){
			$tmpData -=  $max_bj;
		 }else{
			$tmpData -=  $persen_bj * $tmpData * 0.01;
		 }
		 
		 if( ( $persen_bj * $pajaks['pajak_08'] * 0.01 ) > ( $max_bj  ) ){
			$tmpData -=  $max_bj;
		 }else{
			$tmpData -=  $persen_bj * $pajaks['pajak_08'] * 0.01;
		 }

		$tmpData -= $pajaks['pajak_12'];
		$tmpData=$tmpData *12 ; //disetahunkan
		$tmpData -= $pajaks['pajak_17']; //disetahunkan 	
		$tmpData = 100 * floor( $tmpData / 100 );
		$outstanding = $tmpData;
		$pph=0;
		 
		 //TARIF
 
		foreach($arange as $key=>$value){
			$outs=MIN($outstanding,$value);
			$pph +=$outs*$atarif[$key]* 0.01 ;
			$outstanding-=$outs;
		}		 
		 
		 if( $outstanding > 0){
			$pph +=$outs*$atarif[$max_tarif]* 0.01 ;
		 }
		unset($tmpData);
		$pph = $pph/12; //per bulan
		$pph = 100 * ( floor( $pph /100) ); 	
		$tmpData = $pph -  $tpj; //per bulan
		 
		 if($tmpData  == 0){
			break;
		 }
		 
		 if( $y == 0 ){ 
			$gross = $tmpData;
		 }
		 
		else{ 
			 if( ( $gross > 0 AND  $tmpData < 0 ) OR ( $gross < 0  AND $tmpData > 0 ) ){
				break;
			 }
		 }
		
		 if( $gross > 0 ){
			$tpj +=  100;
		 }else{
			$tpj -=  100;
			
		}
			  
		//ENDFOR
		
	} 
	return $tpj;
}

function get_kolom_pajak($id){
	$query = "
		SELECT value FROM pr_konfigurasi_pajak 
		WHERE kolom_id = {$id} ";
	$res = my_query($query);
	$rw = my_fetch_array($res); 
	return $rw['value'];
}

function get_pph21($pkp){	
	$query = "SELECT * FROM pr_tarif_pajak
				ORDER BY label_range" ;
	$result=my_query($query);
	$i=$pph=$tarif=$outs=0;
	$outstanding=$pkp;
	while($ey = my_fetch_array($result)){
		if($i>0){
			$outs=MIN($outstanding,(int) $ey['label_range']);
			$pph +=$outs*$tarif* 0.01 ;
			$outstanding-=$outs;
		}
		$i++;
		$tarif=$ey['tarif'] ;
	}	
	if( $outstanding > 0){
		$pph +=$outs*$tarif* 0.01 ;
	}
	return $pph ;
}

function get_sk_id($tanggal,$komponen_gaji_id){	
	$query = "
		SELECT sk_id FROM pr_sk_tunjangan 
		WHERE komponen_gaji_id =".$komponen_gaji_id." 
		AND tanggal<='".$tanggal."' 
		ORDER BY tanggal DESC LIMIT 1
		";
	$rw = my_fetch_array(my_query($query));
	$sk_id= isset( $rw['sk_id'] ) ? $rw['sk_id']:  0;
	return $sk_id ;
	
}	

function kalkulasi_gaji_all(){	
	$query1 = "DELETE FROM pr_gaji  
		WHERE periode_id=0";
	$result1 = my_query($query1); //reset
	
	$qmax1 = "SELECT MAX(gaji_id) as pr_gaji_max_id FROM pr_gaji";
	$rmax1 = my_query($qmax1);
	$emax1 = my_fetch_array($rmax1);
	$newmax1 = $emax1['pr_gaji_max_id'] + 1;
	my_query("ALTER TABLE  `pr_gaji` AUTO_INCREMENT = {$newmax1}");
	
	
	$query2 = "DELETE FROM pr_gaji_komponen 
		WHERE periode_id=0";
	$result2 = my_query($query2); //reset
	
	$qmax2 = "SELECT MAX(gaji_komponen_id) as pr_gaji_komponen_max_id FROM pr_gaji_komponen";
	$rmax2 = my_query($qmax2);
	$emax2 = my_fetch_array($rmax2);
	$newmax2 = $emax2['pr_gaji_komponen_max_id'] + 1;
	my_query("ALTER TABLE  `pr_gaji_komponen` AUTO_INCREMENT = {$newmax2}");
	
	
	$query3 = "DELETE FROM pr_gaji_pajak 
		WHERE periode_id=0";
	$result3 = my_query($query3); //reset
	$qmax3 = "SELECT MAX(gaji_pajak_id) as pr_gaji_pajak_max_id FROM pr_gaji_pajak";
	$rmax3 = my_query($qmax3);
	$emax3 = my_fetch_array($rmax3);
	$newmax3 = $emax3['pr_gaji_pajak_max_id'] + 1;
	my_query("ALTER TABLE  `pr_gaji_pajak` AUTO_INCREMENT = {$newmax3}");
	
	
	$end=get_batas_tmt() ;
	/*
	$query = " SELECT a.karyawan_id FROM karyawan a
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
		";	
	*/
	$query = " SELECT a.karyawan_id FROM karyawan a
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			WHERE  a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian ) "; 
	$result = my_query($query); 
	$konstan=get_konstan();	
	while($ey = my_fetch_array($result)){
		kalkulasi_gaji($ey['karyawan_id'], $konstan) ;
	}
	return true;
}	

function get_tanggal_by_periode_id(){
	$periode_id =get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periodes['tanggal']));
	$tahun = $periodes['tahun'] ;
	return sprintf( '%02d',$bulan).'-'.$tahun ;
}
 

function page_kalkulasi(){
	my_set_code_js('
	$(document).ready(function() {
       	$(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&task=kalkulasi_gaji_all&'.rand(0,9).'='.rand(0,99999).'\'); 
	});
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}

function get_konstan(){
	$hasil=array();
	$hasil['HARI_BULAN_BERIKUT']=formula_get_hari_bulan_berikut() ;
	$hasil['HARI_BULAN_SEBELUM'] = formula_get_hari_bulan_sebelum();
	$hasil['HARI_BULAN'] = formula_get_hari_bulan();
	$hasil['HARI_KONSUMSI'] = formula_get_hari_bulan_konsumsi();
	$hasil['HARI_NORMAL_11'] = 20;//formula_get_hari_kerja_normal_11();
	$hasil['HARI_BULAN_SEBELUM_11'] = 20;//formula_get_hari_bulan_sebelum_11() ;
	return $hasil ;
}	
function formula_get_hari_bulan_berikut(){	
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	if($bulan =='12' ){
		$bulan = '01';
		$tahun ++ ;	
	}else{
		$bulan++ ;
	}		
	$startdate=$tahun.'-'.sprintf( '%02d',$bulan) .'-01';
	$tgl_akhir=date('d', strtotime(get_last_date_by_month($tahun,$bulan))) ;

	$enddate=$tahun.'-'.sprintf( '%02d',$bulan) .'-'.$tgl_akhir;
 
	$res=jumlah_hari_not_weekend($startdate , $enddate  );
	return $res;
}	

function formula_get_hari_bulan_sebelum(){
	$p_id =get_periode_absen();
	$periode_id=get_last_periode_id($p_id) ;
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$tgl_akhir=date('d', strtotime(get_last_date_by_month($tahun,$bulan))) ;
	$enddate=$tahun.'-'.sprintf( '%02d',$bulan) .'-'.$tgl_akhir;
	$startdate=$tahun.'-'.sprintf( '%02d',$bulan) .'-01';
	$res=jumlah_hari_not_weekend($startdate , $enddate  );
//	$res-=count_hari_libur($startdate , $enddate  )	;
	return $res;
}	

function formula_get_hari_bulan_sebelum_11(){
	$dates=get_startdate_enddate();
	$enddate=$dates['enddate'];
	$startdate=$dates['startdate'];
	$res=jumlah_hari_not_weekend($startdate , $enddate  );
	return $res;
}	


function formula_get_hari_bulan_konsumsi(){
	$query = "SELECT MAX(periode_id) AS periode_max_id FROM pr_gaji_komponen";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$periode_id = $row['periode_max_id'];
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$tgl_akhir=date('d', strtotime(get_last_date_by_month($tahun,$bulan))) ;
	$enddate=$tahun.'-'.sprintf( '%02d',$bulan) .'-'.$tgl_akhir;
	$startdate=$tahun.'-'.sprintf( '%02d',$bulan) .'-01';
	$res=jumlah_hari_not_weekend($startdate , $enddate  );
	return $res;
}	

function formula_get_hari_bulan(){
	$periode_id =get_periode_gaji();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$tgl_akhir=date('d', strtotime(get_last_date_by_month($tahun,$bulan))) ;
	$enddate=$tahun.'-'.sprintf( '%02d',$bulan) .'-'.$tgl_akhir;
	$startdate=$tahun.'-'.sprintf( '%02d',$bulan) .'-01';
	$res=jumlah_hari_not_weekend($startdate , $enddate  );
	return $res;
}	


function formula_get_hari_kerja_normal_11(){
	$dates=get_startdate_enddate();
	$enddate=$dates['enddate'];
	$startdate=$dates['startdate'];
	$res=jumlah_hari_not_weekend($startdate , $enddate  );
	$res-=count_hari_libur($startdate , $enddate  )	;
	return $res;
}


function get_gaji_dibayar($gapok,$tmb){
	$gaji_dibayar=$gapok ;
	list($yyyy,$mm,$dd) = explode("-" , $tmb);
	$bulan_tmb = (int) date('m' , strtotime($tmb));
	$tahun_tmb = (int) date('Y' , strtotime($tmb));	
	$day_out = 0;
	if( $mm == $bulan_gaji AND $$yyyy==$tahun_gaji ) {
		if($dd > 1){
			for ($i=1; $i<=$dd-1; $i++){
				$tmb= $tahun_tmb.'-'.sprintf( '%02d',$bulan_tmb).'-'.sprintf( '%02d',$i) ;
				if( is_sabtu_minggu($tmb) ){
				}else{
					$day_out++;
				}	
			}
			if( $day_out > 0 ){
				$gaji_dibayar=round($gapok - round($day_out * 8/173 * $gapok)) ;
			}
		}
	}	
	return $gaji_dibayar;
}

function thp_minus(){
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'TMB'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Gol'=>array('style'=>'text-align:left;border-bottom:2px solid;width:10%'),  
		'Take Home Pay'=>array('style'=>'text-align:right;border-bottom:2px solid;width:35%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	$end=get_batas_tmt() ;
	if(isset($_GET['key'])){
		$query = "
			SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			INNER JOIN pr_gaji d ON d.karyawan_id=a.karyawan_id AND d.periode_id =0 AND d.takehomepay<0 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			AND c.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}'
		";
	}else{	
		$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				INNER JOIN pr_gaji d ON d.karyawan_id=a.karyawan_id AND d.periode_id =0 AND  d.takehomepay<0 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$query = " SELECT takehomepay FROM pr_gaji 
			WHERE periode_id=0
			AND karyawan_id=".$ey['karyawan_id']
		;
		$result1 = my_query($query);	
		$gajis = my_fetch_array($result1);
		if(	$gajis ){
			$takehomepay = $gajis['takehomepay'];	
		}else{
			$takehomepay = 0;
		}
		
		$detailproperty = array(
			'href'=>'index.php?com=pr_adjustment&task=detail&karyawan_id='.$ey['karyawan_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'gd'=>  $karyawan['nama_gelar'] , 
			'tmb'=> date('d-m-Y',strtotime($karyawan['tmb'])),
			'gol'=>  $karyawan['golongan_jabatan'] , 
			'kalkulasi'=>  position_text_align (rp_format($takehomepay ) ,'right'),      
			'operasi'=> position_text_align( $detail_button, 'right'),
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.get_tanggal_by_periode_id().'</span>'   , "<b>Periode Gaji</b>"    ); 
	$view  .= form_footer(  );
	$menuontop = array(  
		'Back' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_adjustment'.'\'')
	);
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana">
	<b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop ,$search ); 
	$paging = $kgPagerOBJ ->showPaging();
	return  $view.$printmenu.table_builder($header , $datas ,  6 , false , $paging  );
}


function list_excel(){
	$query = "SELECT a.komponen_gaji_id,a.nama_komponen_gaji 
			FROM pr_komponen_gaji a 
			ORDER BY a.komponen_gaji_id"
	;
	$res_komponen = my_query($query);
	$temp['NIK']=array();
	$temp['Nama']=array();
	$temp['Gaji']=array();
	while($ey = my_fetch_array($res_komponen)){
		$temp[$ey['nama_komponen_gaji']]=array();
	}
	$header = $temp;
	$end=get_batas_tmt() ;

	$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	
	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$query = " SELECT gaji_dibayar FROM pr_gaji 
			WHERE periode_id=0
			AND karyawan_id=".$ey['karyawan_id']
		;
		$result1 = my_query($query);	
		$gajis = my_fetch_array($result1);
		if(	$gajis ){
			$gaji_dibayar = $gajis['gaji_dibayar'];	
		}else{
			$gaji_dibayar = 0;
		}
		$det=array();
		$det[0]=position_text_align ($karyawan['nik'] ,'center');
		$det[1]=$karyawan['nama_gelar'] ;
		$det[2]=$gaji_dibayar ;
		$query = "SELECT a.komponen_gaji_id,a.nama_komponen_gaji 
				FROM pr_komponen_gaji a 
				ORDER BY a.komponen_gaji_id"
		;
		$res_komponen = my_query($query);
		while($ey1 = my_fetch_array($res_komponen)){
			$det[$ey1['komponen_gaji_id']+2]= 0 ;
		}
		$query = "SELECT komponen_id,kalkulasi, rapel 
			FROM pr_gaji_komponen  
			where karyawan_id={$ey['karyawan_id']} 
			and periode_id=0" ;
		$res_nilai = my_query($query);
		while($ey2 = my_fetch_array($res_nilai)){
			$det[$ey2['komponen_id']+2]= $ey2['kalkulasi']+$ey2['rapel'] ;
		}	
		$row[] = $det;
	}

	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  24 ,false );
}

function get_rapel_komponen($karyawan_id,$periode_gaji,$komponen_gaji,$tase,$nilai_baru){
	$query = "SELECT kalkulasi FROM pr_gaji_komponen WHERE karyawan_id={$karyawan_id} 
		AND periode_id={$periode_gaji}  
		AND komponen_id={$komponen_gaji}";
	$res = my_query($query);
	$ey = my_fetch_array($res);
	$nilai_lama= (isset($ey['kalkulasi']) ? (int) $ey['kalkulasi'] : 0);
	//$nilai_baru = round( $nilai_lama * $tase ,0 );
	$rapel= $nilai_baru - $nilai_lama ;
	return $rapel ;
}	
